/**
 * 
 */
package com.gf.pf.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.gf.common.BaseController;
import com.gf.common.ResultMessage;
import com.gf.common.UUIDGenerator;
import com.gf.common.model.Annex;
import com.gf.pf.model.Dictionary;
import com.gf.pf.model.Flexigrid;
import com.gf.pf.model.Ztree;
import com.gf.pf.service.DictionaryService;
import com.gf.pf.service.MyBatisService;

/**
 * @description 字典管理
 * @date   Mar 10, 2015
 * @author WLxing
 */
@Controller
@RequestMapping("/pf/dictionary")
public class DictionaryController extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(DictionaryController.class);
	
	/**
	 * 字典列表
	 */
	private String dictionary_list = "pf/dictionaryList";
	
	/**
	 * 添加字典信息
	 */
	private String dictionary_add = "pf/dictionaryAdd";
	
	/**
	 * 修改字典信息
	 */
	private String dictionary_modify = "pf/dictionaryModify";
	
	/**
	 * 每页记录数
	 */
	private String page_sql_id = "pf.dictionary.dicList";
	
	/**
	 * 总记录数
	 */
	private String total_sql_id = "pf.dictionary.total";
	
	/**
	 * 父字典ID为空！
	 */
	private String parent_id_empty = "字典父ID为空！";
	
	/**
	 * 部门ID为空！
	 */
	private String dictionary_id_empty = "字典ID为空！";
	
	@Autowired
	private DictionaryService dictionaryService;

	@Autowired
	private MyBatisService myBatisService;
	
	/**
	 * 
	 * @description 字典管理列表
	 * @return String
	 */
	@RequestMapping(value = "/flDictionary", method = RequestMethod.GET)
	public String dictionaryList() {
		return this.dictionary_list;
	}
	
	/**
	 * 
	 * @description 字典管理列表数据
	 * @return Flexigrid
	 */
	@RequestMapping(value = "/dataGrid", method = RequestMethod.POST)
	@ResponseBody
	public Flexigrid dataGrid(Dictionary dic, Flexigrid flex) {
		if(StringUtils.isEmpty(dic.getId())) {
			dic.setId(super.ROOT_ID);
		}
		flex.setObj(dic);
		flex.setRowStart((flex.getPage() - 1) * flex.getRp());
		flex.setRowEnd(flex.getPage() * flex.getRp());
		List<?> rows = myBatisService.getList(this.page_sql_id, flex);
		int total = myBatisService.count(this.total_sql_id, flex);
		flex.setTotal(total);
		flex.setRows(rows);
		return flex;
	}
	
	/**
	 * 
	 * @description 字典树形数据
	 * @return List<Ztree>
	 */
	@RequestMapping(value = "/ztree", method = RequestMethod.POST)
	@ResponseBody
	public List<Ztree> getDictionaryZtree() {
		return dictionaryService.getDictionaryZtree();
	}
	
	/**
	 * @description 添加字典跳转
	 * @return String
	 */
	@RequestMapping(value = "/faDictionary", method = RequestMethod.GET)
	public String dictionaryAddForward(String pId, Model model) {
		if(!StringUtils.isEmpty(pId)) {
			Dictionary dic = dictionaryService.getDic(pId);
			model.addAttribute("dic", dic);
		} else {
			LOGGER.error(this.parent_id_empty);
			return this.dictionary_list;
		}
		return this.dictionary_add;
	}
	
	/**
	 * @description 添加字典信息
	 * @return String
	 */
	@RequestMapping(value = "/aDictionary", method = RequestMethod.POST)
	@ResponseBody
	public ResultMessage dicAdd(Dictionary dic) {
		if(StringUtils.isEmpty(dic.getDicParentId())) {
			LOGGER.error(this.parent_id_empty);
		}
		
		dic.setId(UUIDGenerator.getInstance().getUUID32());
		Integer result = dictionaryService.saveDic(dic);
		if(result > 0) {
			super.resultMessage.setFlag(super.RESULTMESSAGE_SUCCESS);
			super.resultMessage.setMsg("字典保存成功！");
		} else {
			super.resultMessage.setFlag(super.RESULTMESSAGE_FAIL);
			super.resultMessage.setMsg("部门保存失败！");
		}
		return super.resultMessage;
	}
	
	/**
	 * @description 字典信息修改跳转
	 * @return String
	 */
	@RequestMapping(value = "/fmDictionary", method = RequestMethod.GET)
	public String depModifyForward(String dicId, Model model) {
		if(!StringUtils.isEmpty(dicId)) {
			Dictionary dic = dictionaryService.getDic(dicId);
			model.addAttribute("dic", dic);
		} else {
			LOGGER.error(this.dictionary_id_empty);
			return this.dictionary_list;
		}
		return this.dictionary_modify;
	}
	
	/**
	 * @description 字典修改
	 * @return ResultMessage
	 */
	@RequestMapping(value = "/mDictionary", method = RequestMethod.POST)
	@ResponseBody
	public ResultMessage dicModify(Dictionary dic) {
		if(StringUtils.isEmpty(dic.getId())) {
			LOGGER.error(this.dictionary_id_empty);
		}
		
		Integer result = dictionaryService.modifyDic(dic);
		if(result > 0) {
			super.resultMessage.setFlag(super.RESULTMESSAGE_SUCCESS);
			super.resultMessage.setMsg("字典修改成功！");
		} else {
			super.resultMessage.setFlag(super.RESULTMESSAGE_FAIL);
			super.resultMessage.setMsg("字典修改失败！");
		}
		return super.resultMessage;
	}
	
	/**
	 * 
	 * @description 删除字典信息
	 * @return ResultMessage
	 */
	@RequestMapping(value = "/dDictionary", method = RequestMethod.POST)
	@ResponseBody
	public ResultMessage dicDelete(String dicId) {
		if(StringUtils.isEmpty(dicId)) {
			LOGGER.error(this.dictionary_id_empty);
		}
		
		Integer result = dictionaryService.deleteDic(dicId);
		if(result > 0) {
			super.resultMessage.setFlag(super.RESULTMESSAGE_SUCCESS);
			super.resultMessage.setMsg("字典删除成功！");
		} else {
			super.resultMessage.setFlag(super.RESULTMESSAGE_FAIL);
			super.resultMessage.setMsg("字典删除失败！");
		}
		return super.resultMessage;
	}
	
	
	/**
	 * 处理异步文件上传请求
	 */
	@RequestMapping("/ajaxUploadFile")
	@ResponseBody
	public void ajaxUploadFile(@RequestParam(value = "file") MultipartFile file, HttpServletResponse response){
		boolean status = true;
		String msg = "上传成功";
		System.out.println("%%%%%%%" + file.getName());
		String filePath = file.getName();
		String returns = "{\"status\":" + status + ",\"msg\":\"" + msg + "\",\"value\":\"" + filePath.replaceAll("\\\\", "/") + "\"}";
		
		try {
			response.setContentType("text/html;charset=UTF-8");
			response.getWriter().write(returns);
		} catch (IOException e) {
			new RuntimeException("ajax返回出错");
			//logger.error("ajax返回出错");
		}
	}
	
}
